package com.huizhonglm.diap.presenters.wallet;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.wallet.AliOrderParams;
import com.huizhonglm.diap.api.model.wallet.AliOrderResponse;
import com.huizhonglm.diap.api.model.wallet.RechargeParams;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.wallet.iView.RechargeIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2015-07-07 11:19
 */
public class RechargePresenter {

    private RechargeIView iView;

    public RechargePresenter(RechargeIView iView) {
        this.iView = iView;
    }

    public void getAliOrderInfo(double totalFee){
        AliOrderParams params = new AliOrderParams(API.buildUri(API.ALI_ORDER_BY_RECHARGE));
        params.userId = UserUtil.getUserId();
        params.totalFee = totalFee;
        API.http().post("getAliOrderInfo", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    AliOrderResponse resp = JSON.parseObject(result, AliOrderResponse.class);
                    iView.getAliOrderSuccess(resp);
                } else {
                    iView.getAliOrderFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.getAliOrderFailure();
            }
        });
    }

    public void onRecharge(RechargeParams params) {
        API.http().post("recharge", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())){
                        iView.onRechargeSuccess();
                    } else {
                        iView.onRechargeFailure("充值失败，请联系客服");
                    }
                } else {
                    iView.onRechargeFailure("充值失败，请联系客服");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onRechargeFailure("充值失败，请联系客服");
            }
        });
    }
}
